import time
from datetime import datetime

from wxauto import WeChat

class Bot:

    def __init__(self,listener_nicknames,func):
        # 初始化微信对象
        self.bot = WeChat()
        self.listener_nicknames = listener_nicknames
        self.func = func
        print(f'开始监听消息:[{listener_nicknames}]')
        for nick_name in listener_nicknames:
            self.bot.AddListenChat(who= nick_name)
        print("微信初始化完成")

    def hold(self):
        while True:
            msgs = self.bot.GetListenMessage()
            for name,chats in msgs.items():
                self.func(name,chats)
            time.sleep(1)

    def send_message(self,nickname, message):
        try:
           # 发送消息
           self.bot.SendMsg(message,nickname)
        except Exception as e:
           print(f'发送消息失败:{e}')




